/**
 * Copyright 2021 xieyuanhui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.milantiejiang.tuto;

import android.util.Log;

public class Utils {
    private static final String TAG = Utils.class.getSimpleName();

    /**
     * 数组转换成十六进制字符串
     *
     * @param bArray
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制字节转为字符串
     * 例：[0x34,0x36,0x30,0x30,0x37]  --> 46007
     *
     * @param operator
     */
    public static String hexBytes2Str(byte[] operator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < operator.length; i++) {
            char c = (char) operator[i];
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * int转换为小端byte[]（高位放在高地址中）
     *
     * @param iValue
     * @return
     */
    public static byte[] int2Bytes_LE(int iValue) {
        byte[] rst = new byte[4];
        // 先写int的最后一个字节
        rst[0] = (byte) (iValue & 0xFF);
        // int 倒数第二个字节
        rst[1] = (byte) ((iValue & 0xFF00) >> 8);
        // int 倒数第三个字节
        rst[2] = (byte) ((iValue & 0xFF0000) >> 16);
        // int 第一个字节
        rst[3] = (byte) ((iValue & 0xFF000000) >> 24);
        Log.i("int2Bytes_LE", bytesToHexString(rst));
        return rst;
    }

    /**
     * 转换byte数组(小端)转为int
     *
     * @return
     * @note 数组长度至少为4，按小端方式转换,即传入的bytes是小端的，按这个规律组织成int
     */
    public static int bytes2Int_LE(byte[] bytes) {
        if (bytes.length < 4) {
            return -1;
        }
        int iRst = (bytes[0] & 0xFF);
        iRst |= (bytes[1] & 0xFF) << 8;
        iRst |= (bytes[2] & 0xFF) << 16;
        iRst |= (bytes[3] & 0xFF) << 24;
        return iRst;
    }

    /**
     * 截取byte数组   不改变原数组
     * @param b 原数组
     * @param off 偏差值（索引）
     * @param length 长度
     * @return 截取后的数组
     */
    public static byte[] subByte(byte[] b,int off,int length){
        byte[] b1 = new byte[length];
        System.arraycopy(b, off, b1, 0, length);
        return b1;
    }

    public static byte[] buildMsg(byte[] dataLen, byte[] data) {
        byte[] packedMsg = new byte[4 + 4 + 4 + 4 + data.length + 4];
        System.arraycopy(CommContent.HEADER, 0, packedMsg, 0, 4);
        System.arraycopy(dataLen, 0, packedMsg, 4, 4);
        System.arraycopy(data, 0, packedMsg, 8, data.length);
        System.arraycopy(CommContent.TRAIL, 0, packedMsg, 16 + data.length, 4);
        Log.i(TAG, bytesToHexString(packedMsg));
        return packedMsg;
    }

}
